package com.landinfo.web.rest;

import com.alibaba.fastjson.JSON;
import com.codahale.metrics.annotation.Timed;
import com.landinfo.domain.User;
import com.landinfo.domain.UserPermission;
import com.landinfo.repository.UserPermissionRepository;
import com.landinfo.repository.UserRepository;
import com.landinfo.service.MailService;
import com.landinfo.service.UserPermissionService;
import com.landinfo.service.UserService;
import com.landinfo.service.mapper.UserMapper;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.web.rest.vm.ManagedUserVM;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api")
public class UserPermissionResource {
    private final Logger log = LoggerFactory.getLogger(UserResource.class);

    private static final String ENTITY_NAME = "UserPermission";

    private final UserPermissionService userPermissionService;
    private final UserPermissionRepository userPermissionRepository;


    public UserPermissionResource(UserPermissionService userPermissionService,
                                  UserPermissionRepository userPermissionRepository) {

        this.userPermissionService = userPermissionService;
        this.userPermissionRepository = userPermissionRepository;
    }

    @PostMapping("/userPermission/create")
    @Timed
    public String create(@RequestBody UserPermission userPermission) throws URISyntaxException {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (userPermission == null) {
            hashMap.put("success", false);
            hashMap.put("message", "传入参数为空");
            JSON.toJSONString(hashMap);
        }
        Object object = this.userPermissionRepository.save(userPermission);
        hashMap.put("success", true);
        hashMap.put("rows", object);
        return JSON.toJSONString(hashMap);
    }

    @DeleteMapping("/userPermission/del/{id}")
    @Timed
    public String delete(@PathVariable String id) throws URISyntaxException {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (id == null || "".equals(id)) {
            hashMap.put("success", false);
            hashMap.put("message", "传入参数为空");
            return JSON.toJSONString(hashMap);
        }
        try {
            hashMap.put("success", true);
            this.userPermissionRepository.deleteById(id);

        } catch (Exception ex) {
            hashMap.put("success", false);
            hashMap.put("message", ex.getMessage());
        }
        return JSON.toJSONString(hashMap);
    }

    @PostMapping("/userPermission/update")
    @Timed
    public String update(@RequestBody UserPermission userPermission) throws URISyntaxException {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (userPermission == null) {
            hashMap.put("success", false);
            hashMap.put("message", "传入参数为空");
            JSON.toJSONString(hashMap);
        }
        Object object = this.userPermissionService.update(userPermission);
        hashMap.put("success", true);
        hashMap.put("rows", object);
        return JSON.toJSONString(hashMap);
    }

    @GetMapping("/userPermission/query/{id}")
    @Timed
    public String query(@PathVariable String id) {
//        UserPermission userPermission = this.userPermissionRepository.findOne(id);
        UserPermission userPermission = this.userPermissionRepository.findOneById(id);
        if (userPermission == null) {
            return null;
        } else {
            return JSON.toJSONString(userPermission);
        }
    }

    @GetMapping("/userPermission/query")
    @Timed
    public String query() {
        List<UserPermission> userPermissions = this.userPermissionRepository.findAll();
        return JSON.toJSONString(userPermissions);
    }


}
